using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VKSMA
    {
        public DataRow Convert2KSMA(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryKSMA(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["KSMAMANDANT"] = " ".PadRight(2 ); 
         dwReturn["KSMAJHJJ"] = " ".PadRight(0 ); 
         dwReturn["KSMAMM"] = " ".PadRight(0 ); 
         dwReturn["KSMAKSTKRS"] = " ".PadRight(2 ); 
         dwReturn["KSMAKST"] = " ".PadRight(4 ); 
         dwReturn["KSMAART"] = " ".PadRight(6 ); 
         dwReturn["KSMAAENZL"] = " ".PadRight(0 ); 
         dwReturn["KSMAAENB"] = " ".PadRight(8 ); 
         dwReturn["KSMAAEND"] = " ".PadRight(0 ); 
         dwReturn["KSMANEUB"] = " ".PadRight(8 ); 
         dwReturn["KSMANEUD"] = " ".PadRight(0 ); 
         dwReturn["KSMAFJ"] = " ".PadRight(5 ); 
         dwReturn["KSMAPER"] = " ".PadRight(0 ); 
         dwReturn["KSMABUDGET"] = " ".PadRight(0 ); 
         dwReturn["KSMABEST"] = " ".PadRight(0 ); 
         dwReturn["KSMAIST"] = " ".PadRight(0 ); 
         dwReturn["KSMAFILLER"] = " ".PadRight(7 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapKSMA(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveKSMA(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryKSMA(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapKSMA(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertKSMA(dwLocal, ITXDBVERSION);
            }
            UpdateKSMA(dwLocal);
        }
        public void InsertKSMA(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO KSMA(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryKSMA(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM KSMA WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM KSMA  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "KSMA";
            Return = Result;
            return Return;
        }
        public void DeleteKSMA(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM KSMA   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateKSMA(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE KSMA SET "
                    +"KSMAMANDANT= :KSMAMANDANT,"
                    +"KSMAJHJJ= :KSMAJHJJ,"
                    +"KSMAMM= :KSMAMM,"
                    +"KSMAKSTKRS= :KSMAKSTKRS,"
                    +"KSMAKST= :KSMAKST,"
                    +"KSMAART= :KSMAART,"
                    +"KSMAAENZL= :KSMAAENZL,"
                    +"KSMAAENB= :KSMAAENB,"
                    +"KSMAAEND= :KSMAAEND,"
                    +"KSMANEUB= :KSMANEUB,"
                    +"KSMANEUD= :KSMANEUD,"
                    +"KSMAFJ= :KSMAFJ,"
                    +"KSMAPER= :KSMAPER,"
                    +"KSMABUDGET= :KSMABUDGET,"
                    +"KSMABEST= :KSMABEST,"
                    +"KSMAIST= :KSMAIST,"
                    +"KSMAFILLER= :KSMAFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("KSMAMANDANT", OracleType.NVarChar).Value = dwData["KSMAMANDANT"].ToString(); 
          or1.Parameters.Add("KSMAJHJJ", OracleType.NVarChar).Value = dwData["KSMAJHJJ"].ToString(); 
          or1.Parameters.Add("KSMAMM", OracleType.NVarChar).Value = dwData["KSMAMM"].ToString(); 
          or1.Parameters.Add("KSMAKSTKRS", OracleType.NVarChar).Value = dwData["KSMAKSTKRS"].ToString(); 
          or1.Parameters.Add("KSMAKST", OracleType.NVarChar).Value = dwData["KSMAKST"].ToString(); 
          or1.Parameters.Add("KSMAART", OracleType.NVarChar).Value = dwData["KSMAART"].ToString(); 
          or1.Parameters.Add("KSMAAENZL", OracleType.NVarChar).Value = dwData["KSMAAENZL"].ToString(); 
          or1.Parameters.Add("KSMAAENB", OracleType.NVarChar).Value = dwData["KSMAAENB"].ToString(); 
          or1.Parameters.Add("KSMAAEND", OracleType.NVarChar).Value = dwData["KSMAAEND"].ToString(); 
          or1.Parameters.Add("KSMANEUB", OracleType.NVarChar).Value = dwData["KSMANEUB"].ToString(); 
          or1.Parameters.Add("KSMANEUD", OracleType.NVarChar).Value = dwData["KSMANEUD"].ToString(); 
          or1.Parameters.Add("KSMAFJ", OracleType.NVarChar).Value = dwData["KSMAFJ"].ToString(); 
          or1.Parameters.Add("KSMAPER", OracleType.NVarChar).Value = dwData["KSMAPER"].ToString(); 
          or1.Parameters.Add("KSMABUDGET", OracleType.NVarChar).Value = dwData["KSMABUDGET"].ToString(); 
          or1.Parameters.Add("KSMABEST", OracleType.NVarChar).Value = dwData["KSMABEST"].ToString(); 
          or1.Parameters.Add("KSMAIST", OracleType.NVarChar).Value = dwData["KSMAIST"].ToString(); 
          or1.Parameters.Add("KSMAFILLER", OracleType.NVarChar).Value = dwData["KSMAFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
